/*
 * @Author: wuqingchun
 * @Date: 2024-04-23 13:20:32
 * @Description: 虚析构函数的使用
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-04-23 13:41:48
 */
#ifndef __CLASS_VIRTUAL_DESTRUCT_H__
#define __CLASS_VIRTUAL_DESTRUCT_H__

#include <iostream>

// 定义了虚析构函数的基类
class CVritualDestructBase
{
public:
    CVritualDestructBase()
    {
        std::cout << "CVritualDestructBase::CVritualDestructBase()" << std::endl;
    }

    // 虚析构函数
    virtual ~CVritualDestructBase()
    {
        std::cout << "CVritualDestructBase::~CVritualDestructBase()" << std::endl;
    }
};

class CVritualDestructDerived : public CVritualDestructBase
{
public:
    CVritualDestructDerived()
    {
        std::cout << "CVritualDestructDerived::CVritualDestructDerived()" << std::endl;

        m_piData = new int[100];
    }

    ~CVritualDestructDerived()
    {
        std::cout << "CVritualDestructDerived::~CVritualDestructDerived()" << std::endl;
        if (m_piData != nullptr)
        {
            delete [] m_piData;
            m_piData = nullptr;
        }
    }
private:
    int* m_piData {nullptr};
};

// 定义了析构函数不是虚函数的基类
class CDestructBase
{
public:
    CDestructBase()
    {
        std::cout << "CDestructBase::CDestructBase()" << std::endl;
    }

    ~CDestructBase()
    {
        std::cout << "CDestructBase::~CDestructBase()" << std::endl;
    }
};


class CDestructDerived : public CDestructBase
{
public:
    CDestructDerived()
    {
        std::cout << "CDestructDerived::CDestructDerived()" << std::endl;

        m_piData = new int[100];
    }

    ~CDestructDerived()
    {
        std::cout << "CDestructDerived::~CDestructDerived()" << std::endl;
        if (m_piData != nullptr)
        {
            delete [] m_piData;
            m_piData = nullptr;
        }
    }
private:
    int* m_piData {nullptr};
};

#endif //__CLASS_VIRTUAL_DESTRUCT_H__